package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ISalaryFormulaConditionDAO;
import org.jsola.hr.dto.SalaryFormulaConditionAddDTO;
import org.jsola.hr.dto.SalaryFormulaConditionUpdateDTO;
import org.jsola.hr.entity.SalaryFormulaConditionDO;
import org.jsola.hr.query.SalaryFormulaConditionQuery;
import org.jsola.hr.service.ISalaryFormulaConditionService;
import org.jsola.hr.vo.SalaryFormulaConditionListVO;
import org.jsola.hr.vo.SalaryFormulaConditionVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrSalaryFormulaConditionServiceImpl")
public class SalaryFormulaConditionServiceImpl implements ISalaryFormulaConditionService {

    @Autowired
    private ISalaryFormulaConditionDAO salaryFormulaConditionDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public SalaryFormulaConditionVO save(SalaryFormulaConditionAddDTO salaryFormulaConditionAddDTO, TokenUser tokenUser) {
        // 转DO
        SalaryFormulaConditionDO salaryFormulaConditionDO = salaryFormulaConditionAddDTO.to(SalaryFormulaConditionDO.class);
        // 保存
        salaryFormulaConditionDO = save(salaryFormulaConditionDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return salaryFormulaConditionDO.to(SalaryFormulaConditionVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public List<SalaryFormulaConditionVO> batchSave(List<SalaryFormulaConditionAddDTO> salaryFormulaConditionAddDTOList, TokenUser tokenUser) {
        if(CollectionUtils.isEmpty(salaryFormulaConditionAddDTOList)) {
            return null;
        }
        List<SalaryFormulaConditionDO> salaryFormulaConditionDOList = salaryFormulaConditionAddDTOList
                .parallelStream()
                .map(salaryFormulaConditionAddDTO -> {
                    SalaryFormulaConditionDO salaryFormulaConditionDO = salaryFormulaConditionAddDTO.to(SalaryFormulaConditionDO.class);
                    salaryFormulaConditionDO.preInsert(tokenUser.getUserId());
                    salaryFormulaConditionDO.setSiteId(tokenUser.getSiteId());
                    return salaryFormulaConditionDO;
                }).collect(Collectors.toList());
        salaryFormulaConditionDAO.insertListAndSetId(salaryFormulaConditionDOList);
        return  salaryFormulaConditionDOList
                .parallelStream()
                .map(salaryFormulaConditionDO -> salaryFormulaConditionDO.to(SalaryFormulaConditionVO.class))
                .collect(Collectors.toList());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(SalaryFormulaConditionUpdateDTO salaryFormulaConditionUpdateDTO, TokenUser tokenUser) {
        //转DO
        SalaryFormulaConditionDO salaryFormulaConditionDO = salaryFormulaConditionUpdateDTO.to(SalaryFormulaConditionDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(salaryFormulaConditionDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... salaryFormulaConditionIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) salaryFormulaConditionIds);
    }


    @Override
    public int deleteByExample(Example example, TokenUser tokenUser) {
        example.and().andEqualTo("siteId",tokenUser.getSiteId());
        return salaryFormulaConditionDAO.deleteByExample(example);
    }

    @Override
    public SalaryFormulaConditionVO selectById(String salaryFormulaConditionId, String siteId) {
        SalaryFormulaConditionDO salaryFormulaConditionDO = selectDOById(salaryFormulaConditionId, siteId);
        if (salaryFormulaConditionDO == null) {
            return null;
        }
        return salaryFormulaConditionDO.to(SalaryFormulaConditionVO.class);
    }

    @Override
    public List<SalaryFormulaConditionListVO> select(SalaryFormulaConditionQuery salaryFormulaConditionQuery, String siteId) {
        List<SalaryFormulaConditionDO> salaryFormulaConditionDOList = selectDO(salaryFormulaConditionQuery, siteId);
        if(CollectionUtils.isEmpty(salaryFormulaConditionDOList)) {
            return salaryFormulaConditionDOList == null ? null : new ArrayList<>();
        }
        return salaryFormulaConditionDOList.stream()
                .map(salaryFormulaConditionDO -> salaryFormulaConditionDO.to(SalaryFormulaConditionListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(SalaryFormulaConditionQuery salaryFormulaConditionQuery, String siteId) {
        Example example = buildExample(salaryFormulaConditionQuery,siteId);
        return salaryFormulaConditionDAO.selectCountByExample(example);
    }

    @Override
    public Page<SalaryFormulaConditionListVO> selectPage(SalaryFormulaConditionQuery salaryFormulaConditionQuery, String siteId) {
        Example example = buildExample(salaryFormulaConditionQuery,siteId);
        Page<SalaryFormulaConditionDO> page = salaryFormulaConditionDAO.selectPageByExample(example,
                salaryFormulaConditionQuery.getPageNo(),
                salaryFormulaConditionQuery.getPageSize());

        return page.to(SalaryFormulaConditionListVO.class);
    }

    @Override
    public SalaryFormulaConditionDO selectDOById(String salaryFormulaConditionId, String siteId) {
        return listById(salaryFormulaConditionId, siteId);
    }

    @Override
    public List<SalaryFormulaConditionDO> selectDO(SalaryFormulaConditionQuery salaryFormulaConditionQuery, String siteId) {
        Example example = buildExample(salaryFormulaConditionQuery,siteId);
        return salaryFormulaConditionDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param salaryFormulaConditionQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(SalaryFormulaConditionQuery salaryFormulaConditionQuery, String siteId) {
        Example example = new Example(SalaryFormulaConditionDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (salaryFormulaConditionQuery.getSalaryPlanId() != null) {
            example.and().andEqualTo("salaryPlanId", salaryFormulaConditionQuery.getSalaryPlanId());
        }
        if (salaryFormulaConditionQuery.getFormulaId() != null) {
            example.and().andEqualTo("formulaId", salaryFormulaConditionQuery.getFormulaId());
        }
        if (salaryFormulaConditionQuery.getPropertyName() != null) {
            example.and().andEqualTo("propertyName", salaryFormulaConditionQuery.getPropertyName());
        }
        if (salaryFormulaConditionQuery.getComparisonOperator() != null) {
            example.and().andEqualTo("comparisonOperator", salaryFormulaConditionQuery.getComparisonOperator());
        }
        if (salaryFormulaConditionQuery.getCondition() != null) {
            example.and().andEqualTo("condition", salaryFormulaConditionQuery.getCondition());
        }
        if (salaryFormulaConditionQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", salaryFormulaConditionQuery.getCompanyId());
        }
        if (salaryFormulaConditionQuery.getGmtCreateStart() != null &&
                salaryFormulaConditionQuery.getGmtCreateEnd() != null) {
            example.and().andBetween("gmtCreate", salaryFormulaConditionQuery.getGmtCreateStart(),
                    salaryFormulaConditionQuery.getGmtCreateEnd());
        }
        if (salaryFormulaConditionQuery.getCreateUserId() != null) {
            example.and().andLike("createUserId", "%" + salaryFormulaConditionQuery.getCreateUserId() + "%");
        }
        if (salaryFormulaConditionQuery.getGmtModifiedStart() != null &&
                salaryFormulaConditionQuery.getGmtModifiedEnd() != null) {
            example.and().andBetween("gmtModified", salaryFormulaConditionQuery.getGmtModifiedStart(),
                    salaryFormulaConditionQuery.getGmtModifiedEnd());
        }
        if (salaryFormulaConditionQuery.getModifiedUserId() != null) {
            example.and().andLike("modifiedUserId", "%" + salaryFormulaConditionQuery.getModifiedUserId() + "%");
        }
        // 排序
        ExampleKit.setExampleOrder(example,salaryFormulaConditionQuery.getOrders());
        return example;
    }
}




